import commerceContractApi from '@/api/commerceContract.api.js'
export default {
  data () {
    return {
      selaTypeList: [], // 印章类型
      productTypeList: [] // 销售产品类型
    }
  },
  created () {
    this.getProductTypeList()
    this.getSelaTypeList()
  },
  methods: {
    // 获取销售产品类型
    getProductTypeList () {
      commerceContractApi.getSelectValueList({
        type: 'SaleProductType'
      }).then(res => {
        if (res.data.success) {
          this.productTypeList = res.data.data
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取印章类型
    getSelaTypeList () {
      commerceContractApi.getSelectValueList({
        type: 'SelaType'
      }).then(res => {
        if (res.data.success) {
          this.selaTypeList = res.data.data
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 合同附件下载
    downloadContract (id) {
      commerceContractApi.DownloadContract({
        id: id
      }).then(res => {
        if (res.headers['content-disposition']) {
          let fileName = res.headers['content-disposition'].split('filename=')[1]
          let blob = new Blob([res.data], { type: 'application/octet-stream' })
          if ('download' in document.createElement('a')) {
            let objectUrl = URL.createObjectURL(blob)
            const link = document.createElement('a')
            link.download = fileName
            link.href = objectUrl
            link.click()
          } else {
            navigator.msSaveBlob(blob, fileName)
          }
        } else {
          this.$Message.warning('找不到对应的文件')
        }
      })
    },
    // 盖章合同下载
    downloadSelaContract (id) {
      commerceContractApi.DownloadSelaContract({
        id: id
      }).then(res => {
        if (res.headers['content-disposition']) {
          let fileName = res.headers['content-disposition'].split('filename=')[1]
          let blob = new Blob([res.data], { type: 'application/octet-stream' })
          if ('download' in document.createElement('a')) {
            let objectUrl = URL.createObjectURL(blob)
            const link = document.createElement('a')
            link.download = fileName
            link.href = objectUrl
            link.click()
          } else {
            navigator.msSaveBlob(blob, fileName)
          }
        } else {
          this.$Message.warning('找不到对应的文件')
        }
      })
    }
  }
}
